<?php

/**
 * 分包任务类
 */

namespace App\Jobs;

use App\Repository\LongFileRepository;
use Carbon\Carbon;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

//use Log;


class Subpackage extends Job
{
    protected $id;
    protected $type;
    public $tries = 3;
    public $log = null;
    public $timeout = 600;

    public function __construct($ids, $type = 'read')
    {
        $this->log = new Logger('subpackage');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/subpackage.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );

        $this->id = $ids;
        $this->type = $type;
//        file_put_contents(storage_path('/logs/x1.log'), $this->type);
    }

    /**
     * 执行任务
     *
     * @return void
     */
    public function handle()
    {
//        file_put_contents(storage_path('/logs/x2.log'), 'aaa');
        ini_set('max_execution_time', 800);
//        $this->log->info('操作333333333333');
        $model = new LongFileRepository();

        $res = $model->readDataAll($this->id, $this->type);

//        $this->log->info('传递的参数xx:' . $this->id . '类型' . $this->type . '返回的参数:' . json_encode($res));
        // sleep(20);
        // Log::info('拆包操作222---' . $this->id);
        // $this->log->info('操作文件 添加进表中');
        // $modelTasking = new WlktaskingRepository();
        // $modelTasking->changeInfo($this->id);
        //$this->job->delete();
    }

    public function retryUntil()
    {
        //任务超时时间
        $time = 24 * 60 * 60;
        return Carbon::now()->addSeconds($time);
    }

    /**
     * 处理失败任务
     *
     * @return void
     */
    public function failed()
    {
        $this->log->info('操作文件失败' . $this->id . '类型' . $this->type);
        // $modelTasking = new WlktaskingRepository();
        // $modelTasking->restore($this->id);
    }
}
